/*
编写 : Hexh
时间 : 2012-7-9 10:48:32
*/
using System.Collections.Generic;
using HMIS.FrameWork.DomainBase;
using HMIS.FrameWork.Repository.Nhb;
using HMIS.Infrastructure.Core.Entities;
using HMIS.Infrastructure.Core.Repositories.Impl;

namespace FsCarInfo.Core.Entity.IRespositories.Impl
{
	/// <summary>
	///	 the repository of FsGatelogList
	/// </summary>
	public class FsGatelogListRepositoryImpl : RepositoryNhbImpl<FsGatelogList>, IFsGatelogListRepository
	{

		/// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <returns>True， False</returns>
		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, null);
		}

		// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <param name="where">Where 条件</param>
		/// <returns>True， False</returns>
		public bool IsFieldExistWhere(string fieldName, string fieldValue, string id, string where)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, where);
		}

		/// <summary>
		/// 查询 FsGatelogList
		/// </summary>
		/// <param name="query">查询条件</param>
		/// <param name="start">分页开始</param>
		/// <param name="limit">分页大小</param>
		/// <param name="sort">排序条件</param>
		/// <param name="dir">排序方向</param>
		/// <param name="filters">筛选条件</param>
		/// <param name="total">返回记录条数</param>
		/// <returns>返回记录集</returns>
		public IList<FsGatelogList> GetPlistAll(string query, int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			// filter
			var strFilter = base.GetHqlstrByExtFilter(filters, "t");

			// todo : 修改查询条件ID1 ID2
			string strQuery = string.IsNullOrEmpty(query) ? strFilter
				: "( t.ID1 = :query or t.ID2 = :query ) "
					+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter);

			// sort
			string strSort = string.IsNullOrEmpty(sort) ? string.Empty : " order by t." + sort + " " + dir;

			// todo : 修改关联查询条件 OuterColumn
			string strSql = @" from FsGatelogList as t 
									left join fetch t.FsGatelog "
				+ (string.IsNullOrEmpty(strQuery) ? string.Empty : " where " + strQuery);

			// 查询
			var selQuery = Session.CreateQuery(@"select t " + strSql + strSort);
			var totQuery = Session.CreateQuery(@"select count(*) " + strSql.Replace(" join fetch ", " join "));

			// 设置查询条件
			if (!string.IsNullOrEmpty(query))
			{
				selQuery.SetString("query", query);
				totQuery.SetString("query", query);
			}

			// 分页
			if (limit > 0)
				selQuery.SetFirstResult(start).SetMaxResults(limit);

			// 汇总并返回
			total = totQuery.UniqueResult<long>();
			return selQuery.List<FsGatelogList>();
		}

		/// <summary>
		/// 保存出入信息
		/// </summary>
		/// <param name="entity"></param>
		public void SaveLog(FsGatelogList entity)
		{
			//读取未通行的记录
			InfraIdcard idCard = new InfraIdcardRepositoryImpl().GetByCardUId(entity.CardUId);
			FsGatelog mainLog = new FsGatelogRepositoryImpl().GetCurGateLogByIdCard(entity.CardUId, "", "");
			FsGateinfo gateInfo = new FsGateinfoRepositoryImpl().Get(entity.GateId);

			//设置信息
			if (mainLog == null || mainLog.Id == null)
			{
				mainLog = new FsGatelog
				{
					CardUid = entity.CardUId,
					CarNo = entity.CarNo,
					Type = idCard.Type,
					EntTime = System.DateTime.Now,
					State = "0"
				};

			}
			new FsGatelogRepositoryImpl().SaveOrUpdate(mainLog);

			entity.LogId = mainLog.Id;
			entity.ApprState = gateInfo.Type.Substring(0, 1);
			this.SaveOrUpdate(entity);
		}

		/// <summary>
		/// 获取当前记录
		/// </summary>
		/// <param name="CardUid"></param>
		/// <param name="ApprState"></param>
		/// <param name="start"></param>
		/// <param name="limit"></param>
		/// <param name="total"></param>
		/// <returns></returns>
		public IList<FsGatelogList> GetCurrentList(string DepartmentId, string GateId, string CardUid, string ApprState, int start, int limit, out long total)
		{
			// 查询未结束的出入记录
			string strSql = @" from FsGatelogList as t left join fetch t.FsGatelog where t.FsGatelog.State = '0' ";
			// 根据ID卡号
			strSql = string.IsNullOrEmpty(CardUid) ? strSql :
				strSql + " and t.FsGatelog.CardUid = :CardUid ";
			// 根据授权状态
			strSql = string.IsNullOrEmpty(ApprState) ? strSql :
				strSql + " and t.ApprState = :ApprState ";
			// 默认按时间排序
			string strSort = " order by t.CreateTime desc";

			var selQuery = Session.CreateQuery(@"select t " + strSql + strSort);
			var totQuery = Session.CreateQuery(@"select count(*) " + strSql.Replace(" join fetch ", " join "));

			// 设置查询条件
			if (!string.IsNullOrEmpty(CardUid))
			{
				selQuery.SetString("CardUid", CardUid);
				totQuery.SetString("CardUid", CardUid);
			}
			if (!string.IsNullOrEmpty(ApprState))
			{
				selQuery.SetString("ApprState", ApprState);
				totQuery.SetString("ApprState", ApprState);
			}

			// 分页
			if (limit > 0)
				selQuery.SetFirstResult(start).SetMaxResults(limit);

			// 汇总并返回
			total = totQuery.UniqueResult<long>();
			return selQuery.List<FsGatelogList>();
		}

		/// <summary>
		/// 查询 FsGatelogList
		/// </summary>
		/// <param name="DepartmentId">闸机的机构ID</param>
		/// <param name="GateId">道闸ID</param>
		/// <param name="CardUid">ID卡</param>
		/// <param name="ApprState">授权状体</param>
		/// <param name="State">通行记录状态</param>
		/// <param name="start">分页开始</param>
		/// <param name="limit">分页大小</param>
		/// <param name="sort">排序条件</param>
		/// <param name="dir">排序方向</param>
		/// <param name="filters">筛选条件</param>
		/// <param name="total">返回记录条数</param>
		/// <returns>返回记录集</returns>
		public IList<FsGatelogList> GetPlist(string DepartmentId, string GateId, string CardUid,
			string ApprState, string State, int start, int limit,
			string sort, string dir, List<DataFilter> filters, out long total)
		{
			// 修改关联查询条件 OuterColumn
			string strSql = @" from FsGatelogList as t 
									left join fetch t.FsGateInfo
									left join fetch t.FsGatelog where 1=1 ";

			// filter
			var strFilter = base.GetHqlstrByExtFilter(filters, "t");

			// sort
			string strSort = string.IsNullOrEmpty(sort) ? " order by t.CreateTime desc " : " order by t." + sort + " " + dir + ", t.CreateTime desc ";

			// 按GateId查询
			strSql = string.IsNullOrEmpty(GateId) ? strSql :
				strSql + " and t.GateId = '" + GateId + "' ";

			// 按CardUid查询
			strSql = string.IsNullOrEmpty(CardUid) ? strSql :
				strSql + " and t.CardUid = '" + CardUid + "' ";

			// 按审批状态ApprState
			strSql = string.IsNullOrEmpty(ApprState) ? strSql :
				strSql + " and t.ApprState = '" + ApprState + "' ";

			// 按是否结束State
			strSql = string.IsNullOrEmpty(State) ? strSql :
				strSql + " and t.FsGatelog.State = '" + State + "' ";

			// 按DepartmentId查询
			strSql = string.IsNullOrEmpty(DepartmentId) ? strSql :
				strSql + " and t.GateId in (select distinct g.Id from FsGateInfo g where g.Departments.Id = '" + DepartmentId + "') ";

			// 添加filter条件
			strSql = string.IsNullOrEmpty(strFilter) ? strSql :
				strSql + " and " + strFilter;

			// 查询
			var selQuery = Session.CreateQuery(@"select t " + strSql + strSort);
			var totQuery = Session.CreateQuery(@"select count(*) " + strSql.Replace("join fetch", " join "));

			// 分页
			if (limit > 0)
				selQuery.SetFirstResult(start).SetMaxResults(limit);

			// 汇总并返回
			total = totQuery.UniqueResult<long>();
			return selQuery.List<FsGatelogList>();
		}

	}
}
